package com.fwj.auth.annotation;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.fwj.auth.api.user.UserService;
import com.fwj.auth.entity.user.UserExtendAccount;
import com.fwj.core.common.constant.AccountConstant;
import com.fwj.core.common.constant.CoreConstant;
import com.fwj.core.common.constant.RegExConstant;
import com.fwj.core.common.execption.CommonException;
import com.fwj.core.common.model.ResponseEnum;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.lang.annotation.*;

/**
 * @Author: fwj
 * @Data: 2022/4/10 1:21
 * @Version 1.0
 * @Description: 注册参数验证注解
 */

@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD,ElementType.FIELD,ElementType.PARAMETER})
public @interface RegisterValidator {



    @Component
    @Aspect
    class RegisterLogic {

        @Autowired
        private UserService userService;

        @Pointcut(value = "@annotation(com.fwj.auth.annotation.RegisterValidator)")
        private void pointCut(){

        }

        @Around("pointCut() && @annotation(registerValidator)")
        public Object before(ProceedingJoinPoint joinPoint, RegisterValidator registerValidator){
            try {
                Object[] args = joinPoint.getArgs();
                if (null == args) throw new CommonException(ResponseEnum.MISS_PARAMETER);
                Class<?> cla = args[0].getClass();
                //如果不为空
                JSONObject jsonObject = JSONObject.parseObject(JSONObject.toJSONString(args[0]));
                UserExtendAccount userExtendAccount = JSONObject.parseObject(jsonObject.getJSONObject("userAccount").toJSONString(),UserExtendAccount.class);
                //构建用户账号实体
                UserExtendAccount resExtendAccount = buildUserExtendAccount(userExtendAccount);
                //校验账号是否存在
                UniqueUser(resExtendAccount);
                //修改入参
                jsonObject.put("userAccount",resExtendAccount);
                args[0] = JSON.toJavaObject(jsonObject, cla);
                return joinPoint.proceed(args);
            } catch (CommonException e){
                throw e;
            } catch (Exception e){
                throw new CommonException(ResponseEnum.DATA_CONVERSION_ERROR);
            } catch (Throwable throwable) {
                throwable.printStackTrace();
            }
            return null;
        }

        private UserExtendAccount buildUserExtendAccount(UserExtendAccount userExtendAccount){
            UserExtendAccount ret = new UserExtendAccount();
            String account = userExtendAccount.getValue();
            String password = userExtendAccount.getPassword();
            ret.setValue(account);
            ret.setPassword(password);
            //匹配电话号码
            if (account.matches(RegExConstant.Phone)){
                AccountConstant accountConstant = AccountConstant.getAccountType(CoreConstant.PHONE);
                ret.setExtendType(CoreConstant.PHONE);
                ret.setExtendTypeSub(accountConstant.getTypeSub());
                ret.setExtendDesc(accountConstant.getDescription());
                return ret;
            }
            //匹配邮箱
            if (account.matches(RegExConstant.Email)){
                String suffix = account.split("@")[1];
                //获取邮箱信息
                AccountConstant accountConstant = AccountConstant.getAccountType(suffix);
                ret.setExtendType(CoreConstant.EMAIL);
                ret.setExtendTypeSub(accountConstant.getTypeSub());
                ret.setExtendDesc(accountConstant.getDescription());
                return ret;
            }
            throw new CommonException(ResponseEnum.ERROR_PARAMETER);
        }

        private void UniqueUser(UserExtendAccount userExtendAccount){
            if (userService.checkUserExists(userExtendAccount)){
                throw new CommonException(ResponseEnum.USER_EXISTS);
            }
        }
    }
}
